<?

if ($DATA['component']['id'] && ($g = (int)param('gallery'))) {

	// image editor

	$_id_component	= $DATA['component']['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value

	$_id_gallery	= D_GALLERY_DATA;

	$_crit = "id_data=$_id_data AND id_parent=$_id_component";

	$_param			= 'edit';				//	ID param name in the URL, used here & @ template

	$_fields = array(
		'title' => array('lang' => true), 
		'descr' => array('large' => true, 'lang' => true), 
	);

	$s = array_extend( array('path' => 'album', 'tn' => FP_TN_DIR, 'w' => 800, 'h' => 600, 'tnw' => 128, 'tnh' => 96, 'imode' => MI_FIT, 'tnmode' => MI_FIT, 'gmode' => MI_FIT, 'gw' => 128, 'gh' => 96, 'gtpb' => 0, 'gparam' => 'gallery', 'param' => 'id', 'show_tn' => 1, 'nav' => 0, 'navigator' => 1, 'circular' => 0, 'autoscroll' => 0, 'fancybox' => 0, 'autogallery' => 0,), $DATA['component']['settings']);

	$s['path'] = createdir(FP_UPLOAD_PATH."$s[path]/$g/");
	createdir($s['path'].$s['tn']);

	if ($a = addslashes(param($_param))) {

		switch (action(array('editor_save'))) {
		
			case 'editor_save':
	
				$dat = array('id_data' => $_id_gallery, 'assoc' => $s['path'], 'name' => $a);
				$key = (substr($s['path'], -1, 1) == '/') ? array(h($s['path']), h(substr($s['path'], 0, -1))) : array(h($s['path']).'/', h($s['path']));
				$key = "'".implode("','", $key)."'";	// 2 versions of path -  with and without trailing slash

				$dat['id_index'] = 
					($descr = datafetch('db_item', tb('data'), "id_data=$_id_gallery AND assoc in ($key) AND name='$a'")) ? 
						$descr['id_index'] : 0;

				dataset('db_delete', tb('data'), "id_data=$_id_gallery AND assoc in ($key) AND name='".h($a)."'");

				$gid = datafetch('db_insert', tb('data'), $dat );


				$_record = array('id_data' => $_id_meta, 'id_parent' => $gid);


				dataset('killmeta', "id_data=$_id_meta AND id_parent=$gid");
				dataset('savemeta', prepareModel($_fields, $_record));

	
				takeImage('image', $s['w'], $s['h'], $s['path'].$a, $s['tnw'], $s['tnh'], $s['path'].$s['tn'].$a, $s['imode'], $s['tnmode']);

				return _transformURL($CURRENT->url, array('remove_param' => $_param));	// end of component edition
				break;
		}
		
		$DATA['object'] = array('value' => '', 'text' => '', 'image' => checkImage($s['path'], $a));

		$key = (substr($s['path'], -1, 1) == '/') ? array(h($s['path']), h(substr($s['path'], 0, -1))) : array(h($s['path']).'/', h($s['path']));
		$key = "'".implode("','", $key)."'";	// 2 versions of path -  with and without trailing slash

		if ($t = datafetch('db_item', tb('data'), "id_data=$_id_gallery AND assoc in ($key) AND name='$a'")) {

			foreach($DATA['langs'] as $lang)
				$DATA['object']['data'][$lang['text']] = datafetch_array('loadmeta', $_id_meta, 
					array('parent' => $t['id'], 'assoc' => $lang['text'], 'getclob' => true));
		}
		
	}

	return false;
	
} else exit;
